package com.alumni_circle.core.web.rbac;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.entity.rbac.UserRole;
import com.alumni_circle.core.entity.rbac.VO.UserRoleSearchVO;
import com.alumni_circle.core.entity.rbac.VO.UserRoleUpdateVO;
import com.alumni_circle.core.entity.rbac.VO.UserRoleViewVO;
import com.alumni_circle.core.service.rbac.impl.UserRoleServiceImpl;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.web.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙小虬
 * @since 2020-08-02 20:43
 */
@Api(tags = "用户角色管理")
@Controller
@RequestMapping("/userRole")
public class UserRoleController extends BaseController<UserRole> {

    @Resource
    UserRoleServiceImpl userRoleService;


    /**
     * 更新用户角色信息
     * @param userRoleUpdateVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateUserRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新用户角色信息", notes = "更新用户角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新用户角色信息",url="/userRole/updateUserRole")
    public ResponseModel updateUserRole(@ApiParam(required = true, name = "userRoleUpdateVO")
                                 @RequestBody UserRoleUpdateVO userRoleUpdateVO) {

       return new ResponseModel<>(userRoleService.updateById(ModelUtils.transform(userRoleUpdateVO,UserRole.class)));
    }

    /**
     * 禁用用户的角色
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/disUserRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "禁用用户的角色", notes = "禁用用户的角色", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="禁用用户的角色",url="/userRole/disUserRole")
    public ResponseModel disUserRole(@ApiParam(required = true, name = "userRoleUpdateVO")
                                        @RequestBody BaseId baseId) {
        UserRole userRole = new UserRole();
        userRole.setId(baseId.getId());
        userRole.setState("1");
        return new ResponseModel<>(userRoleService.updateById(userRole));
    }

    @ResponseBody
    @RequestMapping(value = "/delUserRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除用户的角色", notes = "删除用户的角色", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="删除用户的角色",url="/userRole/delUserRole")
    public ResponseModel removeUserRole(@ApiParam(required = true, name = "userRoleUpdateVO")
                                     @RequestBody BaseId baseId) {
        return new ResponseModel<>(userRoleService.removeById(baseId.getId()));
    }


    /**
     * 更新用户角色信息
     * @param pageBase
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAllUserRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询用户角色信息", notes = "更新用户角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询用户角色信息",url="/userRole/getAllUserRole")
    public ResponseListModel getAllUserRole(@ApiParam(required = true, name = "userRoleUpdateVO")
                                          @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        System.out.println(userRoleService.getAllUserRole());
        List<UserRoleViewVO>list = userRoleService.getAllUserRole();
        PageInfo<UserRoleViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

    @ResponseBody
    @RequestMapping(value = "/searchUserRole", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "搜索用户角色信息", notes = "搜索用户角色信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="搜索用户角色信息",url="/userRole/searchUserRole")
    public ResponseListModel searchUserRole(@ApiParam(required = true, name = "userRoleUpdateVO")
                                            @RequestBody UserRoleSearchVO userRoleSearchVO) {
        PageHelper.startPage(userRoleSearchVO.getPageNum(),userRoleSearchVO.getPageSize());
        List<UserRoleViewVO> list = userRoleService.searchUserRole(userRoleSearchVO.getId(),userRoleSearchVO.getUsername(),userRoleSearchVO.getUpdateName(),userRoleSearchVO.getState(),userRoleSearchVO.getRoleName(),userRoleSearchVO.getUpdateTime());
        PageInfo<UserRoleViewVO> pageInfo = new PageInfo<>(list);
        return new ResponseListModel<>(list,String.valueOf(pageInfo.getTotal()));
    }

}
